<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>会员分类</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../layui/css/layui.css">
  <link rel="stylesheet" href="./css/style.css">

</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">

  
  <div class="layui-card-body">
    <div style="padding: 10px 0">
      <button class="layui-btn layuiadmin-btn-useradmin" data-type="delAll">删除选中</button>
      <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加分类</button>
  </div>

 
  <table class="layui-hide" id="test"  lay-filter="test"></table>
  

  </div>
</div>



 
<table class="layui-hide" id="test"  lay-filter="test"></table>
  

<script src="../layui/layui.js" charset="utf-8"></script>
<script src="../layui/common.js" charset="utf-8"></script>

<script>
layui.use(['laydate','table'], function(){
  var $ = layui.jquery;
  var laydate = layui.laydate;
  //日期范围
  laydate.render({
    elem: '#test6'
    ,range: true
  });

  var table = layui.table;

  table.render({
    elem: '#test'

    ,title:"会员表"

    ,defaultToolbar:[]
    ,url:''
    ,cellMinWidth: 80
    ,cols: [[
      {type:'checkbox'}
      ,{field:'id', title: 'ID',width:80, sort: true}
      ,{field:'username',width:80, title: '排序'}
      ,{field:'username',width:120, title: '分类名称'}
      ,{field:'username',width:120, title: '佣金比例'}
      ,{field:'optons', title: '操作',templet:function(){
        return '<div class="layui-btn-container"><button class="layui-btn layui-btn-normal layui-btn-sm layui-icon-edit layui-icon" lay-event="edit"  data-title="编辑分类"> 编辑</button><button class="layui-btn layui-btn-sm layui-btn-danger layui-icon-delete layui-icon" lay-event="delete"> 删除</button></div>';
      }}
    ]]
    ,page: true
    ,data:[
      {
        id:1,
      },
      {
        id:2,
      }
    ]
  });

table.on('tool(test)', function(obj){ 
  var data = obj.data; //获得当前行数据
  var layEvent = obj.event; 
  var tr = obj.tr; //获得当前行 tr 的DOM对象

  if(layEvent === 'delete'){ //删除
    layer.confirm('真的删除行么', function(index){
      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
      layer.close(index);
      //向服务端发送删除指令
    });
  } else if(layEvent === 'edit'){ //编辑

    active.add();
      
    //同步更新缓存对应的值
    obj.update({
      username: '123'
      ,title: 'xxx'
    });
  }
});


  //头工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    if(obj.event == "allDelete"){

      var data = checkStatus.data;
      if(data.length == 0){
        layer.msg('未选中行数！');
      }else{
        console.log("全部删除代码区");
      }
      
    }

  });


    var active = {
    del:function(){
      var checkStatus = table.checkStatus('test')
      var checkData = checkStatus.data; //得到选中的数据
      if(checkData.length === 0){
        return layer.msg("没有选中数据");
      }
      
      layer.confirm('确定删除吗？', function(index) {
        //执行 Ajax 后重载
        //...
        
        table.reload('test'); //重载表格
        layer.msg('已删除');
      });
    },
    add: function(){
        var title;
        $(this).data('type') === 'add' ? title='添加用户' : title='编辑资料';
        layer.open({
          type: 2
          ,title: title
          ,content: 'userClassifyAdd.html'
          ,maxmin: true
          ,area: ['420px', '500px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){  
            //提交代码区
            //...
            
            table.reload('test'); //数据刷新
            layer.close(index); //关闭弹层
          }
        }); 
      }

  }

  $(".layuiadmin-btn-useradmin").on("click",function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';

  })








});



</script>

<style>
  .layui-table-cell{height: auto  !important;}
</style>
</body>
</html>